package com.ms.action.basic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ms.dao.entity.basic.ResourcesEty;
import com.ms.dao.entity.basic.SysUserEty;
import com.ms.dao.mapper.basic.SysUserDao;

@Controller
@RequestMapping("/HTML/basic/LoginAction/")
public class LoginAction {

	private Logger logger = Logger.getLogger(LoginAction.class);

	@Autowired
	private SysUserDao sysUserDao;

	@RequestMapping(value = "login.action", method = RequestMethod.POST)
	public @ResponseBody
	String login(HttpServletRequest request, HttpServletResponse response, SysUserEty userEty) {
		JSONObject obj = new JSONObject();
		obj.put("success", true);
		request.getSession().setAttribute("UserEty", userEty);
		obj.put("result", "success");
		return obj.toString();
	}

	@RequestMapping("getUserButtons.action")
	public @ResponseBody
	String getUserButtons(HttpServletRequest request, HttpServletResponse response) {
		JSONArray array = new JSONArray();
		SysUserEty userEty = new SysUserEty();
		userEty.setId(0);// 用于存放parentNodeId
		List<ResourcesEty> buttons = sysUserDao.selectUserTree(userEty);
		array.addAll(buttons);
		return array.toString();
	}

	@RequestMapping("getUserTree.action")
	public @ResponseBody String getUserTree(HttpServletRequest request, HttpServletResponse response) {
		JSONArray array = new JSONArray();
		String parantNodeId = request.getParameter("parantNodeId");
		SysUserEty userEty = new SysUserEty();
		userEty.setId(Integer.parseInt(parantNodeId));
		List<ResourcesEty> nodes = sysUserDao.selectUserTree(userEty);
		for (int i = 0; i < nodes.size(); i++) {
			ResourcesEty rsEty = nodes.get(i);
			JSONObject obj = new JSONObject();
			obj.put("id", rsEty.getNodeId());
			obj.put("text", rsEty.getMenuName());
			obj.put("icon", rsEty.getIcon());
			obj.put("type", rsEty.getType());

			if (rsEty.getActionPath() != null && !rsEty.getActionPath().trim().equals("") && rsEty.getType().equals("iframe")) {
				obj.put("leaf", true);
				obj.put("url", rsEty.getActionPath());
			} else if (rsEty.getJsClassFile() != null && !rsEty.getJsClassFile().trim().equals("") && rsEty.getType().equals("JSClass")) {
				obj.put("leaf", true);
				obj.put("jsUrl", rsEty.getJsClassFile());
				obj.put("mainClass", rsEty.getMainClass());
				obj.put("namespace", rsEty.getNamespace());
			} else {
				obj.put("leaf", false);
			}

			array.add(obj);
		}
		return array.toString();
	}

	@RequestMapping("getUserPermission.action")
	public @ResponseBody String getUserPermission(HttpServletRequest request, HttpServletResponse response) {
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		SysUserEty userEty = new SysUserEty();
		JSONObject obj = new JSONObject();
		userEty.setRealName(userDetails.getUsername());
		obj.put("UserInfo", userEty);
		return obj.toString();
	}

	@RequestMapping("logout.action")
	public @ResponseBody String logout(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success", true);
		request.getSession().removeAttribute("UserEty");
		request.getSession().invalidate();
		obj.put("result", "success");
		return obj.toString();
	}

	@ExceptionHandler
	public @ResponseBody
	String handle(Exception e) {
		logger.error("", e);
		JSONObject obj = new JSONObject();
		obj.put("success", true);
		obj.put("result", "error");
		obj.put("info", e.getMessage());
		return obj.toString();
	}
}
